package com.ikas.ai.server.module.appraise.service;

import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ikas.ai.server.module.appraise.dao.AppraiseResultDAO;
import com.ikas.ai.server.module.appraise.dao.AppraiseResultStatisticsDAO;
import com.ikas.ai.server.module.appraise.dto.req.ResultPageReqDTO;
import com.ikas.ai.server.module.appraise.enums.SceneResultTypeEnum;
import com.ikas.ai.server.module.appraise.enums.ScoreLevelEnum;
import com.ikas.ai.server.module.appraise.model.AppraiseResult;
import com.ikas.ai.server.module.appraise.model.AppraiseResultStatistics;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


@Service("tAppraiseResultService")
public class AppraiseResultService extends ServiceImpl<AppraiseResultDAO, AppraiseResult> {


    @Autowired
    private AppraiseResultDAO appraiseResultDAO;
    @Autowired
    private AppraiseResultStatisticsService appraiseResultStatisticsService;
    @Autowired
    private AppraiseResultStatisticsDAO appraiseResultStatisticsDAO;


    public List<AppraiseResult> overViewList() {
        List<Long> viewIds = appraiseResultDAO.overViewIds();
        LambdaQueryChainWrapper<AppraiseResult> wrapper = new LambdaQueryChainWrapper<>(appraiseResultDAO);
        List<AppraiseResult> list = wrapper.in(AppraiseResult::getId, viewIds)
                .orderByDesc(AppraiseResult::getUpdateTime)
                .last("limit 8")
                .list();
//        List<AppraiseResult> list = appraiseResultDAO.overViewList();
        list.forEach(p -> {
            p.setLevel(ScoreLevelEnum.desc(p.getScore()));
        });
        return list;
    }

    /**
     * 分页查询评价结果
     *
     * @param req
     * @return
     */
    public List<AppraiseResult> page(ResultPageReqDTO req) {
        LambdaQueryWrapper<AppraiseResult> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppraiseResult::getType, SceneResultTypeEnum.COMPLETE.getCode());
        queryWrapper.eq(CharSequenceUtil.isNotBlank(req.getSystemNo()), AppraiseResult::getSystemNo, req.getSystemNo());
        queryWrapper.ge(req.getStartTime() != null, AppraiseResult::getCreateTime, req.getStartTime());
        queryWrapper.lt(req.getEndTime() != null, AppraiseResult::getCreateTime, req.getEndTime());
        queryWrapper.lt(req.getEndTime() != null, AppraiseResult::getCreateTime, req.getEndTime());
        queryWrapper.eq(StringUtils.isNotEmpty(req.getMachineNo()), AppraiseResult::getMachineNo, req.getMachineNo());
        List<AppraiseResult> list = appraiseResultDAO.selectList(queryWrapper);
        list.forEach(p -> {
            p.setLevel(ScoreLevelEnum.desc(p.getScore()));
        });
        return list;
    }

    /**
     * 获取机组设备的综合评价结果(最新数据)
     *
     * @param machineNo 机组编号
     * @return list
     */
    public List<AppraiseResult> getAppraiseResultByMachineNo(String machineNo) {
        List<AppraiseResult> list =
                appraiseResultDAO.getAppraiseResultByMachineNo(machineNo, SceneResultTypeEnum.COMPLETE.getCode());
        list.forEach(p -> {
            p.setLevel(ScoreLevelEnum.desc(p.getScore()));
            p.setLevelEn(ScoreLevelEnum.descWord(p.getScore()).name());
        });
        return list;
    }
}